package indi.tshoiasc.simstr.utils;

import net.milkbowl.vault.economy.Economy;
import org.bukkit.Bukkit;
import org.bukkit.plugin.RegisteredServiceProvider;

@SuppressWarnings("deprecation")
public final class VaultOperate {
	private static Economy economy;
	private static boolean supportVault = false;

	public static boolean setupEconomy() {
		RegisteredServiceProvider<Economy> economyProvider = Bukkit.getServicesManager().getRegistration(Economy.class);
		if (economyProvider != null) {
			economy = (Economy) economyProvider.getProvider();
		}
		supportVault = economy != null;
		return supportVault;
	}

	public static boolean isSupportVault() {
		if (economy != null) {
			return true;
		}
		return setupEconomy();
	}

	public static void give(String player, double money) {
		if (economy == null) {
			throw new UnsupportedOperationException("还没有链接到Vault");
		}
		economy.depositPlayer(player, money);
	}

	public static void take(String player, double money) {
		if (economy == null) {
			throw new UnsupportedOperationException("还没有链接到Vault");
		}
		economy.withdrawPlayer(player, money);
	}

	public static double getBalance(String player) {
		if (economy == null) {
			throw new UnsupportedOperationException("还没有链接到Vault");
		}
		return economy.getBalance(player);
	}

	public static boolean has(String player, double money) {
		if (economy == null) {
			throw new UnsupportedOperationException("还没有链接到Vault");
		}
		return getBalance(player) >= money;
	}
}
